package web;

import entity.Album;
import entity.Artista;
import entity.Cancion;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sessionbean.AlbumFacadeRemote;
import sessionbean.ArtistaFacadeRemote;
import sessionbean.CancionFacadeRemote;

/**
 *
 * @author bbaquero
 */
public class MostrarCancion extends HttpServlet {

    @EJB
    private CancionFacadeRemote cancionFacade;
    @EJB
    private ArtistaFacadeRemote artistaFacade;
    @EJB
    private AlbumFacadeRemote albumFacade;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            //Obtener los parametros del request URL.
            String album = request.getParameter("album");
            String art = request.getParameter("arti");
            Artista arti = artistaFacade.find(new Integer(art));
            Album Alb = albumFacade.find(new Integer(album));
            out.println("<html>");
            out.println("<head>");
            out.println("<meta name=\"title\" content=\"))) RAW ((( Letras de " + Alb.getTitulo() + ", de " + arti.getNombre() + "\"/>");
            out.println("<meta name=\"description\" content=\"Contenido lirico del album " + Alb.getTitulo() + ", de " + arti.getNombre() + ". Con sus autores e info adicional! Parte de Rock and Words.\"/>");
            //out.println("<link rel=\"image_src\" href=\"imgs/rock.png\"/>");
            out.println("<style type=\"text/css\">#linx{color:#2362C7;font-family:Verdana,Helvetica,Arial,Sans Serif;font-style:normal;letter-spacing:0.8px; font-size:12px;}</style></head>");
            out.println("<title>))) RAW ((( Letras de " + Alb.getTitulo() + ", de " + arti.getNombre() + "</title>");
            out.println("<link href=\"styleRAW.css\" rel=\"stylesheet\" type=\"text/css\" /></head>");
            out.println("<body>");
            out.println(Resources.setRockBar()); //Barra de navegacion de seccion Rock.



            //Crear barra de informacion superior (Artista y Album)
            out.println("<div id=content><div id=infobar>" +
                    "<center><font color=white size=2><b>Artista: <a href=MostrarAlbum?arti=" + arti.getIdArtista() + " style=\"text-decoration: none;\"><font color=white>" + arti.getNombre() + " </font></a> | | Album: " + Alb.getTitulo() + " (" + Alb.getYear() + ")</b></font> </center></div>");
            
            
            List<Cancion> canc = cancionFacade.findByAlbum(new Integer(album));

            out.println("<br><br>");             

            out.println("<a name=top><br><br>");
            //Creacion del menu de las canciones del album.
            for (Iterator it = canc.iterator(); it.hasNext();) {
                Cancion elem = (Cancion) it.next();
                out.println("<a id=linx href=#" + elem.getNumero() + ">" + elem.getNumero() + ". " + elem.getCancion() + "</a><br>");
            }
            out.println("</a>");
            //Creacion de las letras e informacion de cada cancion.
            for (Iterator it = canc.iterator(); it.hasNext();) {
                Cancion elem = (Cancion) it.next();
                out.println("<a name=\"" + elem.getNumero() + "\"><br><br>");
                out.println("<font color=#2362C7 face=verdana size=3>" + elem.getNumero() + ". </font><font color=#2362C7 face=georgia size=5><i><b>" + elem.getCancion() + "</b> </i></font><font size=2 color=grey>letra de " + elem.getAutor() + "</font>");
                out.println("<br><br><font face=verdana size=2 color=white>" + elem.getLetra() + "</font><br><br><a id=linx href=#top>Volver arriba</a><br><br>");
            }
            
            out.println("<br><br><br>"); //Saltos de linea obligatorios para correcta vision de pagina junto con la barra de navegacion de letras.
            out.println("</div>");
            out.println(Resources.setWordsNav()); //Barra de navegacion de letras.
            out.println("</body></html>");

        } finally {
            out.close();
        }
    }

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

